﻿/*
Copyright (C) 2011 by Matthew Hickson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;

using thinkschematwo.Collections;
using thinkschematwo.IO;

namespace tickalist
{
    public partial class MainForm : Form
    {
        GroupedList list;

        public MainForm()
        {
            InitializeComponent();
        } // MainForm

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.Text = String.Format("{0} v{1}", Application.ProductName, Application.ProductVersion);
            list = null;
        } // MainForm_Load

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ProgramController.DoExit();
        } // exitToolStripMenuItem_Click

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
            openFileDialog1.FileName = "";

            if (DialogResult.OK == openFileDialog1.ShowDialog())
            {
                list = ProgramController.DoOpenFile(openFileDialog1.FileName);
                RenderedList.Text = "";

                TreeNode parentNode = null;

                if (null != list)
                {
                    // Populate treeview...
                    ListSourceData.Nodes.Clear();
                    ArrayList keys = list.GroupKeys;
                    keys.Sort();
                    foreach (String key in keys)
                    {
                        if (String.IsNullOrEmpty(key))
                        {
                            parentNode = ListSourceData.Nodes.Add("Uncategorized");
                        }
                        else
                        {
                            parentNode = ListSourceData.Nodes.Add(key);
                        }

                        ArrayList items = list.GetDataList(key);
                        items.Sort();
                        foreach (String item in items)
                        {
                            parentNode.Nodes.Add(item);
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Could not open list.", "Error");
                }
            }
        } // openToolStripMenuItem_Click

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == printDialog1.ShowDialog())
            {
                printDocument1.PrinterSettings = printDialog1.PrinterSettings;
                printDocument1.Print();
            }
        } // printToolStripMenuItem_Click

        private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RenderedList.Text = "";

            bool groupPrinted = false;

            foreach (TreeNode node in ListSourceData.Nodes)
            {
                if (node.Checked)
                {
                    RenderedList.Text += String.Format("[{0}]", node.Text) + Environment.NewLine;
                    groupPrinted = true;
                }

                foreach (TreeNode childNode in node.Nodes)
                {
                    if (childNode.Checked)
                    {
                        if (!groupPrinted)
                        {
                            RenderedList.Text += String.Format("[{0}]", node.Text) + Environment.NewLine;
                            groupPrinted = true;
                        }
                        RenderedList.Text += String.Format("- {0}", childNode.Text) + Environment.NewLine;
                    }
                }

                if (groupPrinted)
                {
                    RenderedList.Text += Environment.NewLine;
                }

                groupPrinted = false;
            }
        } // printPreviewToolStripMenuItem_Click

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(String.Format("{0} v{1}", Application.ProductName, Application.ProductVersion), "About...");
        } // aboutToolStripMenuItem_Click

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.Graphics.DrawString(RenderedList.Text, RenderedList.Font, Brushes.Black, 100, 100);
        } // printDocument1_PrintPage

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
            saveFileDialog1.FileName = "";

            if (DialogResult.OK == saveFileDialog1.ShowDialog())
            {
                ProgramController.DoSaveFile(saveFileDialog1.FileName, RenderedList.Text);
            }
        } // saveToolStripMenuItem_Click
    }
}
